package com.study.zouchao.superproject.register;

import android.content.Context;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;

import com.study.zouchao.superproject.R;
import com.study.zouchao.superproject.login.view.activity.LoginActivity;
import com.study.zouchao.superproject.register.model.RegisterModel;
import com.study.zouchao.superproject.register.model.RegisterResult;
import com.study.zouchao.superproject.util.App;
import com.study.zouchao.superproject.util.RegexUtils;
import com.study.zouchao.superproject.util.StringUtils;
import com.study.zouchao.superproject.util.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by Administrator on 2017/6/24.
 */

public class RegisterPresenter implements IRegisterContract.IRegisterPresenter {

    private Context mContext;

    private IRegisterContract.IRegisterView  mView;

    private Subscription mSup = null;


    public RegisterPresenter(Context mContext, IRegisterContract.IRegisterView mView) {
        this.mContext = mContext;
        this.mView = mView;
    }

    @Override
    public void confirmRegister(String username, String nickname, String pwd, String ensurePwd) {
        attemptRegister(username, nickname, pwd, ensurePwd);
    }
    private static final String REG_NICKNAME = "^[\\u4E00-\\u9FA5A-Za-z0-9_]+$";


    public void attemptRegister(String username, String nickname, String pwd, String ensurePwd) {
        boolean cancel = false;

        if (!RegexUtils.isValid(username, REG_NICKNAME)) {
            mView.showUsernameError("用户名不允许包含特殊字符");
            cancel = true;
            return;
        }
        if (!RegexUtils.isValid(nickname, REG_NICKNAME)) {
            mView.showNicknameError("昵称不允许包含特殊字符");
            cancel = true;
            return;
        }
        if (!RegexUtils.isValid(pwd, REG_NICKNAME)) {
            mView.showPwdError("密码不允许包含特殊字符");
            cancel = true;
            return;
        }
        if (!RegexUtils.isValid(ensurePwd, REG_NICKNAME)) {
            mView.showEnsurePwdError("密码不允许包含特殊字符");
            cancel = true;
            return;
        }

        if (!cancel && !pwd.equals(ensurePwd)) {
            mView.showEnsurePwdError("两次输入的密码不相同");
            cancel = true;
            return;

        }

        if (!cancel) {
            register(username, nickname, pwd, ensurePwd);
        }
    }

    public boolean isValidStr(String in) {
        if (StringUtils.isEmpty(in))    return false;
        return true;
    }

    private void register(String username, String nickname, String pwd, String ensurePwd) {
        mView.showBaseLoading(true);
        mSup = new RegisterModel()
                .register(getReqParams(username, nickname, pwd))
                .subscribe(new Action1<RegisterResult>() {
                    @Override
                    public void call(RegisterResult registerResult) {
                        ToastUtils.showShort(mContext, registerResult.getMessage());
                        mView.showBaseLoading(false);
                        judgeRegisterResult(registerResult);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                        mView.showBaseLoading(false);
                        mView.showSnackBar("Warn！！访问网络失败、", Snackbar.LENGTH_SHORT);
                    }
                });

    }

    private void judgeRegisterResult(RegisterResult registerResult) {
        switch (registerResult.getCode()) {
            case 22001 :
                ToastUtils.showShort(App.getAppContext(), "注册成功！！");
                mView.finishCurrentRegisterActivity();
                break;
            default:
                mView.showSnackBar("Warn！！"+registerResult.getMessage(), Snackbar.LENGTH_SHORT);
                break;
        }
    }

    private Map<String, String> getReqParams(String username, String nickname, String pwd) {
        Map<String, String> params = new HashMap<>();
        params.put("userName", username);
        params.put("nickName", nickname);
        params.put("password", pwd);
        params.put("balance", "0");

        return params;
    }

    @Override
    public void onDestoryPresnter() {
        //取消登陆任务
        if (mSup != null && !mSup.isUnsubscribed()) {
            mSup.unsubscribe();
//            ToastUtils.showShort(mContext, "取消登陆");
        }
    }
}
